package com.dgshanger.sanhxiaofeisc.ui.nearby

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.amap.api.maps2d.AMapUtils
import com.amap.api.maps2d.model.LatLng
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.AppApi
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.entity.MerchantEntity
import com.dgshanger.sanhxiaofeisc.ui.home.activity.ShopDetailsActivity
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import com.xx.baseuilibrary.adapter.BaseRecycleAdapter
import com.xx.baseuilibrary.adapter.BaseViewHolder
import com.xx.baseuilibrary.mvp.contract.BaseMvpRecycleViewContract
import com.xx.baseuilibrary.mvp.lcec.BaseMvpRecycleViewFragment
import com.xx.baseutilslibrary.network.rx.RxHelper
import com.xx.baseutilslibrary.network.rx.XxBaseHttpObserver
import kotlinx.android.synthetic.main.item_nearby_list.view.*
import java.math.BigDecimal


/**
 * MerchantListFragment
 * 沉迷学习不能自拔
 * Describe：商家列表
 * Created by 雷小星🍀 on 2018/4/18 18:45.
 */
class MerchantListFragment : BaseMvpRecycleViewFragment<MerchantEntity>() {

    override val listLimit: Int
        get() = 1

    override val layoutManager: RecyclerView.LayoutManager
        get() = LinearLayoutManager(mContext)

    override fun createAdapter(): BaseRecycleAdapter<MerchantEntity, out BaseViewHolder<MerchantEntity>> = Adapter()

    override fun createModel(): BaseMvpRecycleViewContract.Model<MerchantEntity> = Model()

    class Model : BaseMvpRecycleViewContract.Model<MerchantEntity> {
        override fun loadData(page: Int, httpObserver: XxBaseHttpObserver<List<MerchantEntity>>?) {
            AppApi.Api()
                    .my_collects(page)
                    .compose(RxHelper.io_main())
                    .compose(RxHelper.start_finish(httpObserver))
                    .subscribe(httpObserver)
        }

    }

    override fun initEvent(view: View?) {
        super.initEvent(view)
        adapter!!.setOnItemClickListener{ position, entity ->
            val intent = Intent(mContext, ShopDetailsActivity::class.java)
            intent.putExtra("shop_id", entity.collect_shop_id)
            intent.putExtra("comment_score",entity.comment_score)
            startActivity(intent)

        }
    }


    private class Adapter : BaseRecycleAdapter<MerchantEntity, ViewHolder>() {
        override fun createViewHolder(view: View?, viewType: Int): ViewHolder = ViewHolder(view!!)

        override fun getItemLayout(viewType: Int): Int = R.layout.item_nearby_list

    }

    private class ViewHolder(view: View) : BaseViewHolder<MerchantEntity>(view) {
        override fun bindView(position: Int, entity: MerchantEntity?) {
            ImageLoader.loadToUrl(itemView.context,itemView.iv_nearby,Constants.getBaseUrl()+entity?.menu_img)
            itemView.tv_nearby_name.text=entity!!.shop_name
            itemView.tv_nearby_grade.text=itemView.context.getString(R.string.tv_grade)+entity.comment_score
            itemView.tv_nearby_category.text=entity.category_name
            itemView.tv_nearby_introduce.text=entity.introduce
            val location = Constants.getLocation()
            val myLatLng = LatLng(location[0].toDouble(), location[1].toDouble())
            val shopLatLng = LatLng(entity.latitude.toDouble(), entity.longitude.toDouble())
            val distanceMeters = AMapUtils.calculateLineDistance(myLatLng, shopLatLng).toDouble()
            val scale = BigDecimal(distanceMeters / 1000).setScale(2, BigDecimal.ROUND_DOWN)
            itemView.tv_nearby_distance.text=scale.toString()+"KM"
        }

    }
}